package com.ruoyi.quartz.task;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpClientUtil;
import com.ruoyi.common.utils.http.HttpUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务调度测试
 */
@Component("getUrl")
public class UrlTask {
    public static final String AUTH_VALUE = "matt";
    //测试地址
//    String url = "http://pc.dev.dotflat.cn/api/scheduler";
//    String crmUrl = "http://crm.dev.dotflat.cn/api/scheduler";
    //正式地址
    @Value("${otherSystems.teachUrl}")
    String url;

    @Value("${otherSystems.crmUrl}")
    String crmUrl;
//    String url = "http://pc.dev.dotflat.cn/api/scheduler";
//    String crmUrl = "http://localhost:8091/api/scheduler";

    public void multipleParams() {
        //定时执行任务，教务系统 和crm的定时任务
        System.out.println("执行开始");
        String time = DateUtils.parseDateToStr("yyyy-MM-dd", new Date());
        Map params = new HashMap();
        params.put("time", time + " 00:00:00");
        try {
            HttpClientUtil.sendPost(url + "/daily", params, AUTH_VALUE).toString();

        } catch (IOException e) {
            e.printStackTrace();
        }


        System.out.println("执行完成");
    }

    public void params() {
        System.out.println("执行开始");
        String time = DateUtils.parseDateToStr("yyyy-MM-dd", new Date());
        Map params = new HashMap();
        params.put("time", time + " 00:00:00");
        try {
            HttpClientUtil.sendPost(url + "/hourly", params, AUTH_VALUE).toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("执行完成");
    }


    public void callCrm() {
        System.out.println("执行开始");
        String time = DateUtils.parseDateToStr("yyyy-MM-dd", new Date());
        Map params = new HashMap();
        params.put("time", time + " 00:00:00");
        try {
            HttpClientUtil.sendPost(crmUrl + "/daily", params, AUTH_VALUE).toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("执行完成");
    }

    public static void main(String[] args) {
        System.out.println(String.format("%.2f", 12.331111));;

    }

    //BigDecimal 截取小数位，四舍五入
    public static BigDecimal ConvertNumber(BigDecimal bigDecimal, int divnum, int num) {
        double a = bigDecimal.doubleValue();
        a = a / divnum;
        String numString = "0.";
        for (int i = 0; i < num; i++) {
            numString += "0";
        }
        DecimalFormat df = new DecimalFormat(numString);
        return new BigDecimal(df.format(a).toString());
    }

    public static double divide(double a, double b, int scale) {
        BigDecimal bd1 = new BigDecimal(Double.toString(a));
        BigDecimal bd2 = new BigDecimal(Double.toString(b));
        return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
